﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Dungeon_Dive
{   
    /// <summary>
    /// This class represents player character.
    /// </summary>
    public class PlayerCharacter : GameEntity
    {
        XMLEngine xmlEngine;
        Log log;
        Game parentGame;

        public int Progress { get; set; }
                
        public List<Elements> Tokens    // player's elemental tokens in a list
        { 
            get
            {
                List<Elements> tlist = new List<Elements>(tokens);
                return tlist;
            }
        }

        /// <summary>
        /// Creates a new PlayerCharacter instance.
        /// </summary>
        /// <param name="game"></param>
        /// <param name="name">Xml file containing model details</param>
        public PlayerCharacter(Game game, String name) : base(game)
        {
            parentGame = game;
            log = (Log)game.Services.GetService(typeof(Log));
            xmlEngine = (XMLEngine)game.Services.GetService(typeof(XMLEngine));
            loadFromXml(name + ".xml");
            
            this.CurrentAnim = "idle";
           
            Scale = 1.0f;
            Rotation = 0.0f;

            CreateBoundingSphere();

            AddToken(RandomElement());

            Progress = 0;
            xmlEngine = (XMLEngine)game.Services.GetService(typeof(XMLEngine));
            if (xmlEngine.OpenFile(".\\XML\\progress.xml"))
            {
                while (xmlEngine.NextElement() != "level");
                Progress = Int32.Parse(xmlEngine.GetAttribute("value"));
                xmlEngine.CloseFile();
            }
        }

        /// <summary>
        /// Loads character info from a file in XML directory
        /// </summary>
        /// <param name="filename">Name of the xml file</param>
        /// <returns></returns>
        private bool loadFromXml(String filename)
        {
            log.Write("loadFromXml(" + filename + ")");
            String anim;
            String elem = "";

            if (!xmlEngine.OpenFile(".\\XML\\" + filename)) return false;

            log.Write("Loaded xml file succesfully.");

            while (elem != "monster") //Read until we find the monster(root) element
            {
                elem = xmlEngine.NextElement();
                log.Write("elem: " + elem);
            }

            elem = xmlEngine.NextElement(); //Get the first element after monster

            while (elem != "monster") //Loop until the closing element
            {
                log.Write("elem: " + elem);
                switch (elem)
                {
                    case "animation":
                        anim = xmlEngine.GetAttribute("name");
                        elem = xmlEngine.NextElement();
                        while (elem != "animation") //Loop until we find the closing animation element
                        {
                            log.Write("elem: " + elem);
                            if (elem == "frame")
                            {
                                log.Write("attrib: " + xmlEngine.GetAttribute("model"));
                                this.AddFrame(anim,
                                    parentGame.Content.Load<Model>(".\\Models\\" + xmlEngine.GetAttribute("model")));
                            }
                            elem = xmlEngine.NextElement();
                        }
                        break;
                }
                elem = xmlEngine.NextElement();
            }
            xmlEngine.CloseFile();
            return true;
        }

    }
}
